package com.chinawayltd.ews.sample.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: greeProject
 * @description 
 * @author: chenxin
 * @create: 2018年4月日 上午10:55:53
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

	public static final char UNDERLINE = '_';

    /**
     * 驼峰格式字符串转换为下划线格式字符串
     * 
     * @param param
     * @return
     */
    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 下划线格式字符串转换为驼峰格式字符串
     * 
     * @param param
     * @return
     */
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

	/**
	 * 日期转换成字符串
	 * @author: chenxin
	 * @date: 2018年4月16日
	 * @param date 日期时间
	 * @param format 日期字符串格式("yyyy-MM-dd HH:mm:ss"、"yyyy-MM-dd")
	 * @return
	 */
	public static String DateToStr(Date date, String format) {
	   SimpleDateFormat sdf = new SimpleDateFormat(format);
	   String str = sdf.format(date);
	   return str;
	}
	
	/**
	 * 获取classpath
	 * @author: chenxin
	 * @date: 2018年4月24日
	 * @return
	 */
	public static String getClasspath(){
		String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../").replaceAll("file:/", "").replaceAll("%20", " ").trim();	
		if(path.indexOf(":") != 1){
			path = File.separator + path;
		}
		return path;
	}
	
    public static void main(String[] args) {
        String aaa = "app_version_fld";
        System.out.println(underlineToCamel(aaa));
        aaa = "appVersionFld";
        System.out.println(camelToUnderline(aaa));
        System.out.println("日期"+DateToStr(new Date(), "yyyyMMddHHmmss"));
    
    }
}
